import axios from 'axios';
import * as config from './configsetting';
const  baseURL = config.BaseHost; 
const timeout = 3000;

function getToken(){

}
const service_withtoken = axios.create({
    baseURL: baseURL,
    timeout:timeout 
  });

service_withtoken.interceptors.request.use(config => {
    config.headers.common["Authorization"] =  getToken();
    return config;
  });
  
const service = axios.create({
    baseURL: baseURL,
    timeout:timeout 
  });

export function tget(url,resolve,errcb){
    return service_withtoken({
        url,
        method:"get"
    }).then(resolve)
    .catch(errcb);
  };


export function tpost(url,data,resolve,errcb){
    return service_withtoken({
        url,
        method:"post",
        data:data
    }).then(resolve)
    .catch(errcb);
  };
  
export function get(url,resolve,errcb){
    return service({
        url,
        method:"get"
    }).then(resolve)
    .catch(errcb);
  };


export function post(url,data,resolve,errcb){
    return service({
        url,
        method:"post",
        data:data
    }).then(resolve)
    .catch(errcb);
};

export function checktoken(vm){
  let token = vm.$store.state.app.token;
  console.log(token);
  let serviceT = vm.axios.create({
    baseURL:baseURL,
    timeout: 2000
  });
  let url = '/checktoken';
  serviceT.interceptors.request.use(config => {
    config.headers.common["Authorization"] =  token;
    return config;
  });
  serviceT({
    url,
    method:"get"
  }).then((res,err)=>{
    if(err){
      console.log(err);
      vm.$router.push({path:'/center'});
    }else{
      console.log(res);
      console.log('success check pass');
      console.log(res.data.codestatus);
      if(res.data.codestatus && res.data.codestatus==408){
        vm.$router.push({path:'/center'});
      }
    }
  }).catch(
      err=>console.log(err)
  );
}